<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 观察者
        class Watcher {
            update() {
                console.log('update')
            }
        }
        // 被观察者/发布者
        class Dep {
            constructor() {
                // 记录所有观察者
                this.subs = []
            }

            addSub(sub) {
                if (sub && sub.update) {
                    this.subs.push(sub)
                }
            }

            notify() {
                this.subs.forEach(watcher => {
                    watcher.update()
                })
            }
        }


        const watcher1 = new Watcher()
        const watcher2 = new Watcher()
        const dep = new Dep()
        dep.addSub(watcher1)
        dep.addSub(watcher2)
        dep.notify()
    </script>
</body>
</html>